<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="主体名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入主体名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="主体类型" prop="type">
        <el-select v-model="queryParams.type" @change="handleQuery" placeholder="请选择主体类型" clearable>
          <el-option
            v-for="dict in dict.type.business_entity_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="主体属性" prop="attr">
        <el-select v-model="queryParams.attr" @change="handleQuery" placeholder="请选择主体属性" clearable style="width: 240px">
          <el-option
            v-for="dict in dict.type.business_entity_attribute"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="主体状态" prop="status">
        <!-- 认证状态（0-未认证，1-已认证，2-审核中，3-审核失败） -->
        <el-select v-model="queryParams.status" @change="handleQuery" placeholder="请选择认证状态" clearable style="width: 240px">
          <el-option label="正常" value="0" />
          <el-option label="已注销" value="2" />
          <el-option label="已停产" value="1" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:entity:delete']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:entity:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主体名称" align="center" prop="name" />
      <el-table-column label="认证账号" align="center" prop="account" />
      <el-table-column label="主体属性" align="center" prop="attr" >
        <template slot-scope="scope">
          <span v-for="obj in dict.type.business_entity_attribute">
            <span v-if="obj.value === scope.row.attr"> {{ obj.label }}</span>
          </span>
        </template>
      </el-table-column>
      <el-table-column label="主体类型" align="center" prop="type" >
        <template slot-scope="scope">
          <span v-for="obj in dict.type.business_entity_type">
            <span v-if="obj.value === scope.row.type"> {{ obj.label }}</span>
          </span>
        </template>
      </el-table-column>
      <el-table-column label="主体状态" align="center" prop="status" >
        <template slot-scope="scope">
          <!-- 状态（0-正常，1-停产，2-注销） -->
          <el-tag v-if="scope.row.status === '0'" type="success" size="mini" >正常</el-tag>
          <el-tag v-if="scope.row.status === '1'" type="warning" size="mini" >停产</el-tag>
          <el-tag v-if="scope.row.status === '2'" type="danger" size="mini" >注销</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="认证时间" align="center" prop="authTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <router-link :to="'/information/details/index/entity/' + scope.row.userId + '/' + scope.row.entityId" v-hasPermi="['business:entity:search']">
            <el-button size="mini" type="text" icon="el-icon-info" >主体详情</el-button>
          </router-link>
          <el-button size="mini" v-if="scope.row.status === '0'" type="text" icon="el-icon-warning"
            @click="handleStop(scope.row)" v-hasPermi="['business:entity:stop']" style="margin-left: 10px;">停产</el-button>
          <el-button size="mini" v-if="scope.row.status === '0'" type="text" icon="el-icon-warning"
            @click="handleCancel(scope.row)" v-hasPermi="['business:entity:cancel']" style="margin-left: 10px;">注销</el-button>
          <el-button size="mini" v-if="scope.row.status !== '0'" type="text" icon="el-icon-success"
                     @click="handleToNormal(scope.row)" v-hasPermi="['business:entity:stop', 'business:entity:cancel']" style="margin-left: 10px;">恢复正常</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete"
            @click="handleDelete(scope.row)" v-hasPermi="['business:entity:delete']" style="margin-left: 10px;" >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {entityList, delEntityInfo, stopEntity, cancelEntity, entityToNormal} from "@/api/business/entity";

export default {
  name: "Users",
  dicts: ['business_entity_type', 'business_entity_attribute'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 信息表格数据
      infoList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        type: null,
        attr: null,
        status: null,
      },
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询生产人信息列表 */
    getList() {
      this.loading = true
      entityList(this.queryParams).then(response => {
        this.infoList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.entityId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 停产按钮操作 */
    handleStop(row) {
      const entityId = row.entityId;
      this.$modal.confirm('是否确认停产？').then(function() {
        return stopEntity(entityId)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("停产成功！")
      }).catch(() => {})
    },
    /** 注销按钮操作 */
    handleCancel(row) {
      const entityId = row.entityId;
      this.$modal.confirm('是否确认注销该主体？').then(function() {
        return cancelEntity(entityId)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("注销成功！")
      }).catch(() => {})
    },
    /** 恢复正常按钮 */
    handleToNormal(row) {
      const entityId = row.entityId;
      this.$modal.confirm('是否确认将该主体恢复正常？').then(function() {
        return entityToNormal(entityId)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("操作成功！")
      }).catch(() => {})
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const entityIds = row.entityId || this.ids
      this.$modal.confirm('是否确认删除所选数据？').then(function() {
        return delEntityInfo(entityIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功！")
      }).catch(() => {})
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download('entity/info/export', {
        ...this.queryParams
      }, `info_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
